package com.fpx.order.plat.bussiness.plat.ebay.task;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fpx.order.plat.bussiness.dao.log.IPullOrderLogDao;
import com.fpx.order.plat.bussiness.entity.CustomerShop;
import com.fpx.order.plat.bussiness.entity.PullOrderLog;
import com.fpx.order.plat.bussiness.entity.PullorderLogFail;
import com.fpx.order.plat.bussiness.plat.base.PlatOrderCurrentJob;
import com.fpx.order.plat.bussiness.plat.ebay.service.EbayPlatOrderHandler;
import com.fpx.order.plat.bussiness.system.PlatConstants;
import com.fpx.order.plat.bussiness.vo.OrderDealResult;
/**
 * 
 * @author xiangtao
 *
 */
@Component
public class EbayReceiveOrderJob extends PlatOrderCurrentJob{
	
	private final static Logger LOGGER = Logger.getLogger(EbayReceiveOrderJob.class);
	
	@Autowired
	private EbayPlatOrderHandler ebayPlatOrderHandler;
	
	@Autowired
	private IPullOrderLogDao pullOrderLogDao;
	

	
	/**
	 * 定时任务
	 */
	public void exec(){
		LOGGER.info("【EBAY】 自动拉单开始.....");
		initMeodth(PlatConstants.ORDER_SOURCE_EBAY);
	    List<CustomerShop> customerLst=super.getCustomers();
	    PullOrderLog pullOrderLog=new PullOrderLog();
		if(!org.springframework.util.StringUtils.isEmpty(customerLst)){
		// 遍历商家和店铺
		 for (CustomerShop customer : customerLst) {
			 List<OrderDealResult> orderDealResultLst=null;
			 String cm_id=customer.getCm_id();
			 String cs_id=customer.getCs_id();
		     try {
		    	 pullOrderLog.setFk_cm_id(customer.getCm_id());//客户id
				 pullOrderLog.setFk_cs_code(customer.getCs_id());//商铺ID
				 pullOrderLog.setCs_platform_code(PlatConstants.ORDER_SOURCE_EBAY);//平台号
				 Date beginTime = new Date(); 
				 pullOrderLog.setPull_date_begin(dateFormat.format(beginTime));//拉单开始时间
//		    	if(cm_id.equals("526872")&&cs_id.equals("478")){
		    		orderDealResultLst = ebayPlatOrderHandler.getOrderByCustomer(customer, null, null, 0);
		    		if(orderDealResultLst==null||orderDealResultLst.size()<=0){
		    				throw new RuntimeException("【EBAY】---商家：[" + cm_id + "]-商铺ID:["+cs_id+"],没有获取到订单");
		    		}
//		    	}
		     }catch (Exception ex) {
		       LOGGER.error("【EBAY】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【异常信息】: "+ex.getMessage());
		       pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_N);//拉单失败
		       Date endTime = new Date(); 
		       pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
		       try{
		    	  PullorderLogFail LogFail=new PullorderLogFail();
		    	  LogFail.setError_msg(ex.getMessage());
		    	  pullOrderLogDao.savePullOrderLog(pullOrderLog);
		    	  LogFail.setFk_pol_id(pullOrderLog.getPol_id());
		    	  pullOrderLogDao.savePullLogFail(LogFail);
		       }catch(Exception e){
		    	   LOGGER.error("【EBAY】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存EBAY拉单失败日志时异常】  "+e.getMessage());
		       }
		     }
		     if(orderDealResultLst !=null&& orderDealResultLst.size()>0){
		    	 Date endTime = new Date(); 
			     pullOrderLog.setPull_date_end(dateFormat.format(endTime));//拉单结束时间
			     pullOrderLog.setPull_sign(PlatConstants.IS_PULL_SIGN_Y);
			     try{
			    	 pullOrderLogDao.savePullOrderLog(pullOrderLog);
			     }catch(Exception e){
			    	 LOGGER.error("【EBAY】---商家:["+cm_id+"]-商铺ID:["+cs_id+"]-【保存EBAY拉单成功日志时异常】  "+e.getMessage());
			     }
		     }
		 } 
	 }
       LOGGER.info("【EBAY】 自动拉单结束");
  }

}
